package Assignment.Test2_3;

import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        //创建集合
        ArrayList<Employee> list = new ArrayList<>();

        //创建对象
        Employee e1 = new Employee("张三",3000);
        Employee e2 = new Employee("李四",3500);
        Employee e3 = new Employee("王五",4000);
        Employee e4 = new Employee("赵六",4500);
        Employee e5 = new Employee("田七",5000);

        //添加进集合
        list.add(e1);
        list.add(e2);
        list.add(e3);
        list.add(e4);
        list.add(e5);

        //判断是否有姓名为“王五”的员工，如果有，改名为“王小五”
        int index =  indexEmployee(list,"王五");
        if (index >= 0){
            list.get(index).setName("王小五");
        }

        //判断是否有姓名为“赵六”的员工，如果有，将其删除
        index =  indexEmployee(list,"赵六");
        if (index >= 0){
            list.remove(index);
        }

        //给姓名为“田七”的员工，涨500工资
        index =  indexEmployee(list,"田七");
        if (index >= 0){
            int salary = list.get(index).getSalary() +500;
            list.get(index).setSalary(salary);
        }

        //遍历
        for (int i = 0; i < list.size(); i++) {
            Employee e = list.get(i);
            System.out.println("姓名："+e.getName()+"，工资："+e.getSalary());

        }

    }
    public static int indexEmployee(ArrayList<Employee> list,String name){
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i).getName();
            if (name.equals(str)){
                return i;
            }
        }
        return -1;
    }
}
